/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Dsrv6.persistence.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Dsrv6.persistence.entities.Register;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import Dsrv6.persistence.controllers.exceptions.NonexistentEntityException;
import Dsrv6.persistence.entities.InsuranceType;

/**
 *
 * @author a381u296
 */
public class InsuranceTypeJpaController implements Serializable {

    public InsuranceTypeJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(InsuranceType insuranceType) {
        if (insuranceType.getRegisterList() == null) {
            insuranceType.setRegisterList(new ArrayList<Register>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Register> attachedRegisterList = new ArrayList<Register>();
            for (Register registerListRegisterToAttach : insuranceType.getRegisterList()) {
                registerListRegisterToAttach = em.getReference(registerListRegisterToAttach.getClass(), registerListRegisterToAttach.getRegId());
                attachedRegisterList.add(registerListRegisterToAttach);
            }
            insuranceType.setRegisterList(attachedRegisterList);
            em.persist(insuranceType);
            for (Register registerListRegister : insuranceType.getRegisterList()) {
                InsuranceType oldInstInstIdOfRegisterListRegister = registerListRegister.getInstInstId();
                registerListRegister.setInstInstId(insuranceType);
                registerListRegister = em.merge(registerListRegister);
                if (oldInstInstIdOfRegisterListRegister != null) {
                    oldInstInstIdOfRegisterListRegister.getRegisterList().remove(registerListRegister);
                    oldInstInstIdOfRegisterListRegister = em.merge(oldInstInstIdOfRegisterListRegister);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(InsuranceType insuranceType) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            InsuranceType persistentInsuranceType = em.find(InsuranceType.class, insuranceType.getInstId());
            List<Register> registerListOld = persistentInsuranceType.getRegisterList();
            List<Register> registerListNew = insuranceType.getRegisterList();
            List<Register> attachedRegisterListNew = new ArrayList<Register>();
            for (Register registerListNewRegisterToAttach : registerListNew) {
                registerListNewRegisterToAttach = em.getReference(registerListNewRegisterToAttach.getClass(), registerListNewRegisterToAttach.getRegId());
                attachedRegisterListNew.add(registerListNewRegisterToAttach);
            }
            registerListNew = attachedRegisterListNew;
            insuranceType.setRegisterList(registerListNew);
            insuranceType = em.merge(insuranceType);
            for (Register registerListOldRegister : registerListOld) {
                if (!registerListNew.contains(registerListOldRegister)) {
                    registerListOldRegister.setInstInstId(null);
                    registerListOldRegister = em.merge(registerListOldRegister);
                }
            }
            for (Register registerListNewRegister : registerListNew) {
                if (!registerListOld.contains(registerListNewRegister)) {
                    InsuranceType oldInstInstIdOfRegisterListNewRegister = registerListNewRegister.getInstInstId();
                    registerListNewRegister.setInstInstId(insuranceType);
                    registerListNewRegister = em.merge(registerListNewRegister);
                    if (oldInstInstIdOfRegisterListNewRegister != null && !oldInstInstIdOfRegisterListNewRegister.equals(insuranceType)) {
                        oldInstInstIdOfRegisterListNewRegister.getRegisterList().remove(registerListNewRegister);
                        oldInstInstIdOfRegisterListNewRegister = em.merge(oldInstInstIdOfRegisterListNewRegister);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = insuranceType.getInstId();
                if (findInsuranceType(id) == null) {
                    throw new NonexistentEntityException("The insuranceType with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            InsuranceType insuranceType;
            try {
                insuranceType = em.getReference(InsuranceType.class, id);
                insuranceType.getInstId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The insuranceType with id " + id + " no longer exists.", enfe);
            }
            List<Register> registerList = insuranceType.getRegisterList();
            for (Register registerListRegister : registerList) {
                registerListRegister.setInstInstId(null);
                registerListRegister = em.merge(registerListRegister);
            }
            em.remove(insuranceType);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<InsuranceType> findInsuranceTypeEntities() {
        return findInsuranceTypeEntities(true, -1, -1);
    }

    public List<InsuranceType> findInsuranceTypeEntities(int maxResults, int firstResult) {
        return findInsuranceTypeEntities(false, maxResults, firstResult);
    }

    private List<InsuranceType> findInsuranceTypeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(InsuranceType.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public InsuranceType findInsuranceType(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(InsuranceType.class, id);
        } finally {
            em.close();
        }
    }

    public int getInsuranceTypeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<InsuranceType> rt = cq.from(InsuranceType.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
